package so.shanku.essential.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import net.tsz.afinal.json.JsonMap;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.nostra13.universalimageloader.core.ImageLoader;
import so.shanku.essential.R;
import so.shanku.essential.app.MyApplication;
import so.shanku.essential.utils.Constant;
import so.shanku.essential.utils.ImageLoaderBitmapCallBack;
import so.shanku.essential.utils.StringUtil;

import java.util.List;
import java.util.Map;

/**
 * Created by Jaelyn on 2015/9/19 0019
 * 购物车商品列表显示
 */
public class ShopProductListAdapter extends MyBaseAdapter {
    /**
     * 父类的位置
     */
    private int parentPosition = -1;

    public int getParentPosition() {
        return parentPosition;
    }

    public void setParentPosition(int parentPosition) {
        this.parentPosition = parentPosition;
    }

    public ShopProductListAdapter(Context context) {
        super(context);
    }

    public ShopProductListAdapter(Context context, List<? extends Map<String, ?>> datas) {
        super(context, datas);
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        GoodsListAdapterViewHolder goodsListAdapterViewHolder;
        if (view == null) {
            view = layoutInflater.inflate(R.layout.shopcart_goods_item, null);
            goodsListAdapterViewHolder = new GoodsListAdapterViewHolder();
            ViewUtils.inject(goodsListAdapterViewHolder, view);
            view.setTag(goodsListAdapterViewHolder);
        } else {
            goodsListAdapterViewHolder = (GoodsListAdapterViewHolder) view.getTag();
        }
        setItemTag(i, goodsListAdapterViewHolder);
        bindDatas(i, goodsListAdapterViewHolder);
        return view;
    }

    private void bindDatas(int i, GoodsListAdapterViewHolder goodsListAdapterViewHolder) {
        JsonMap jsonMap = (JsonMap) datas.get(i);
        goodsListAdapterViewHolder.select_goods_cb.setSelected(jsonMap.getBoolean("isChecked"));
        String pic_path = jsonMap.getStringNoNull("picPath");
        ImageLoader.getInstance().displayImage(pic_path, goodsListAdapterViewHolder.goods_logo, MyApplication.getInstance().getDefaultOption(), new ImageLoaderBitmapCallBack());

        boolean isEnable = jsonMap.getBoolean("isEnable");
        if (isEnable) {
            int stock = jsonMap.getInt("stock");
            if (stock < 0) {
                goodsListAdapterViewHolder.sales_over.setVisibility(View.VISIBLE);
                goodsListAdapterViewHolder.sales_over.setText(R.string.sales_off);
            } else {
                goodsListAdapterViewHolder.sales_over.setVisibility(View.GONE );
            }
        } else {
            goodsListAdapterViewHolder.sales_over.setVisibility(View.VISIBLE);
            goodsListAdapterViewHolder.sales_over.setText(R.string.salve_over);
        }


        goodsListAdapterViewHolder.num_add_tv.setEnabled(isEnable);
        goodsListAdapterViewHolder.goods_name_tv.setText(jsonMap.getStringNoNull("salesName"));
        goodsListAdapterViewHolder.goods_price_tv.setText(StringUtil.getFormatMoneyWithSign(jsonMap.getStringNoNull("salePrice")));
        goodsListAdapterViewHolder.goods_discribe_tv.setText(jsonMap.getStringNoNull("speStrName"));
        goodsListAdapterViewHolder.num_tv.setText(jsonMap.getStringNoNull("amount"));
    }

    private void setItemTag(int i, GoodsListAdapterViewHolder goodsListAdapterViewHolder) {
        goodsListAdapterViewHolder.select_goods_cb.setTag(i);
        goodsListAdapterViewHolder.goods_logo.setTag(i);
        goodsListAdapterViewHolder.goods_name_tv.setTag(i);
        goodsListAdapterViewHolder.goods_price_tv.setTag(i);
        goodsListAdapterViewHolder.goods_discribe_tv.setTag(i);
        goodsListAdapterViewHolder.num_sub_tv.setTag(i);
        goodsListAdapterViewHolder.num_add_tv.setTag(i);
        goodsListAdapterViewHolder.num_tv.setTag(i);
    }

    private class GoodsListAdapterViewHolder {

        /**
         * 已售罄
         */
        @ViewInject(R.id.sales_over)
        TextView sales_over;

        /**
         * 是否选中
         */
        @ViewInject(R.id.select_goods_cb)
        ImageView select_goods_cb;

        @OnClick(R.id.select_goods_cb)
        public void select_goods_cb_click(View view) {
            if (itemCompountClickListener != null) {
                itemCompountClickListener.itemCompountClick(parentPosition, Constant.ShopCartItemCompontType.SELECT_GOODS_ITEM, (Integer) view.getTag());
            }
        }

        /**
         * 商品图标
         */
        @ViewInject(R.id.goods_logo)
        ImageView goods_logo;
        @ViewInject(R.id.product_name_tv)
        TextView goods_name_tv;

        @ViewInject(R.id.goods_price_tv)
        TextView goods_price_tv;
        @ViewInject(R.id.productBrand_tv)
        TextView goods_discribe_tv;
        @ViewInject(R.id.num_sub_tv)
        TextView num_sub_tv;

        @OnClick(R.id.num_sub_tv)
        public void num_sub_tv_click(View view) {
            if (itemCompountClickListener != null) {
                itemCompountClickListener.itemCompountClick(parentPosition, Constant.ShopCartItemCompontType.SUB_NUM, (Integer) view.getTag());
            }
        }

        @ViewInject(R.id.num_tv)
        TextView num_tv;

        @OnClick(R.id.num_tv)
        public void num_tv_click(View view) {
            if (itemCompountClickListener != null) {
                itemCompountClickListener.itemCompountClick(parentPosition, Constant.ShopCartItemCompontType.NUM_EDIT, (Integer) view.getTag());
            }
        }

        @ViewInject(R.id.num_add_tv)
        TextView num_add_tv;

        @OnClick(R.id.num_add_tv)
        public void num_add_tv_click(View view) {
            if (itemCompountClickListener != null) {
                itemCompountClickListener.itemCompountClick(parentPosition, Constant.ShopCartItemCompontType.ADD_NUM, (Integer) view.getTag());
            }
        }
    }

    private ItemCompountClickListener itemCompountClickListener;

    /**
     * 设置控件的点击事件
     *
     * @param itemCompountClickListener
     */
    public void setItemCompountClickListener(ItemCompountClickListener itemCompountClickListener) {
        this.itemCompountClickListener = itemCompountClickListener;
    }

    public interface ItemCompountClickListener {
        /**
         * 点击事件的回调接口
         *
         * @param itemPosition 第几个条目
         * @param type         类型
         */
        void itemCompountClick(int itemPosition, Constant.ShopCartItemCompontType type, int childIndex);
    }
}
